﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DBBase;
using System.IO;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;
using System.IO.Ports;
using System.Security;
using System.Security.Cryptography;
namespace Business
{
    public partial class juesequanxian : Form
    {
        public MainForm m_mainform;
        public static string sSMJY;

        public string[] cquanxian;
        private CheckBox[] cche;

        public string sname1;
        public string sname2;
        public string sname3;

        public string sname;

        public TreeNode nd1;
        public TreeNode nd2;
        public TreeNode nd3;


        public string sjsmc;
        public juesequanxian()
        {
            InitializeComponent();
            treeView11.ExpandAll(); // 展开全部
            treeView12.ExpandAll(); // 展开全部
            treeView13.ExpandAll();  //展开全部
            treeView14.ExpandAll();  //展开全部


            treeView11.Visible=false; // 展开全部
            treeView12.Visible = false; // 展开全部
            treeView13.Visible = false;  //展开全部
            treeView14.Visible = false; //展开全部

            treeView11.Enabled = false; // 展开全部
            treeView12.Enabled = false; // 展开全部
            treeView13.Enabled = false;  //展开全部
            treeView14.Enabled = false; //展开全部

            
            sSMJY = "否";
            sname = "";

            cche = new System.Windows.Forms.CheckBox[20] { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7, checkBox8, checkBox9, checkBox10, checkBox11, checkBox12, checkBox13, checkBox14, checkBox15, checkBox16, checkBox17, checkBox18, checkBox19, checkBox20 };
           
            for (int i = 0; i < 20; i++)
            {
                cche[i].Visible = false;
            }
           
        }
        private string Encryption(string express)//加密
        {
            CspParameters param = new CspParameters();
            param.KeyContainerName = "gzwk2006";
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] plaindate = Encoding.Default.GetBytes(express);
                byte[] encryptdate = rsa.Encrypt(plaindate, false);

                return Convert.ToBase64String(encryptdate);
            }
        }
        //
        private string Decrypt(string cip)//解密
        {
            CspParameters param = new CspParameters();
            param.KeyContainerName = "gzwk2006";
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] encryptdate = Convert.FromBase64String(cip);
                byte[] decryptdate = rsa.Decrypt(encryptdate, false);
                return Encoding.Default.GetString(decryptdate);
            }
        }
        void inicbx(ListView cbx, string dataname)
        {
            cbx.Items.Clear();
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"select * from {0} order by YHLB", dataname);
            DataTable dt = objquery.GetTable(ssqlex);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListViewItem li = new ListViewItem();
                string sxg = "";
                if (dt.Rows[i]["XGJLBS"].ToString().Trim().Contains("是"))
                {
                    sxg = "*";
                }
                else
                {
                    sxg = "";
                }
                var spassword = dt.Rows[i]["PASSWORD"].ToString().Trim();
                try
                {
                    spassword = Decrypt(dt.Rows[i]["PASSWORD"].ToString().Trim());
                }
                catch { }

                var uid = "";
                uid = dt.Rows[i]["UID"].ToString().Trim();
                var YHLB = "";
                YHLB = dt.Rows[i]["YHLB"].ToString().Trim();
               var NAME = "";
               NAME = dt.Rows[i]["NAME"].ToString().Trim();
               var GZRYBH = "";
               GZRYBH = dt.Rows[i]["GZRYBH"].ToString().Trim();
              
               var sdlmima = string.Format(@"{0}{1}{2}", uid, NAME, GZRYBH);
               if (!spassword.Contains(sdlmima))
                {
                    sxg = ".";
                }
                else
                {
                    sxg = "";
                }
                li.SubItems[0].Text = Decrypt(dt.Rows[i]["ZHMJM"].ToString().Trim());
                li.SubItems.Add(dt.Rows[i]["JSLX"].ToString().Trim()+sxg);
                li.SubItems.Add(dt.Rows[i]["YHJS"].ToString().Trim());
                if (dt.Rows[i]["SFMJ"].ToString().Trim().Contains("是"))
                {
                    li.SubItems.Add("是");
                }
                else
                {
                    li.SubItems.Add("否");
                }

                cbx.Items.Add(li);
                
            }
            foreach (ListViewItem lvItems in cbx.Items)
            {
                if (lvItems.SubItems[1].Text.Contains("."))
                {
                    lvItems.ForeColor = Color.Red;//字体颜色
                    m_mainform.baocunaqrj("数据篡改", "写入", "用户数据库", "否", "否", "成功", string.Format("用户数据库的用户{0}数据被篡改", lvItems.SubItems[0].Text));//事件、类型（增、删、该、查）、模块、是否核心功能、是否非常规业务、结果和描述

                }
                else
                {
                    lvItems.ForeColor = Color.Black;//字体颜色
                }
            }
          
        }
        /// <summary>
        /// 数据库中读取所有权限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void getsetTreeNode(TreeNodeCollection nodes)
        {

            foreach (TreeNode td in nodes)
            {
                td.Checked = false;
                nd1 = td;

                
                //  td.Text 读取该node;
                sname1 = td.Text;
                if (td.Nodes.Count > 0)
                    getsetTreeNode2(td.Nodes);
            }
        }

        private void getsetTreeNode2(TreeNodeCollection nodes)//子类
        {
            foreach (TreeNode td in nodes)
            {
                //  td.Text 读取该node;
                td.Checked = false;
                nd2 = td;
                sname2 = td.Text;
                if (td.Nodes.Count > 0)
                {
                    getsetTreeNode3(td.Nodes);
                }
                else
                {
                  
                }
                if (getmoqx(sname2, sjsmc))
                {
                    td.Checked = true;
                    nd1.Expand();
                }
                else
                {
                    td.Checked = false;
                }

            }
        }
        private void getsetTreeNode3(TreeNodeCollection nodes)//三级子类
        {
            foreach (TreeNode td in nodes)
            {
                //  td.Text 读取该node;
                td.Checked = false;
                nd3 = td;
                sname3 = td.Text;
                bool bche = td.Checked;
                if (td.Nodes.Count > 0)
                {
                    getsetTreeNode4(td.Nodes);
                }
                else
                {
                    
                }
                if (getmoqx(sname2 + sname3, sjsmc))
                {
                    td.Checked = true;
                    nd2.Expand();
                }
                else
                {
                    td.Checked = false;
                }
            }
        }
        private void getsetTreeNode4(TreeNodeCollection nodes)//三级子类
        {
            foreach (TreeNode td in nodes)
            {
                //  td.Text 读取该node;
                td.Checked = false;
               
                bool bche = td.Checked;
                if (td.Nodes.Count > 0)
                {
                    getsetTreeNode4(td.Nodes);
                }
                else
                {

                }
               
            }
        }
        public bool getmoqx(string name, string sleixing)
        {

            Query objquery = new Query();
            string ssql = "";
            ssql = string.Format(@"Select * From mokuaiquanxian where qx_name like '%%{0}%%'", name);
            string sfjy = objquery.GetDBString(ssql, "qx_jingyuan");

            ssql = string.Format(@"Select * From juesequanxian where jsqx_jsmc='{0}' and jsqx_qx like '%%{1}%%'", sleixing, name);
            if (objquery.selectDBcount(ssql) > 0)
            {
                if (sfjy.Contains("是"))
                {
                    if (sSMJY.Contains("是"))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }



        }



        private void lstUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem item in lstUser.SelectedItems)
            {
                string test1 = item.SubItems[0].Text.ToString().Trim();
                string test2 = item.SubItems[1].Text.ToString().Trim();//角色名称
                string test3 = item.SubItems[2].Text.ToString().Trim();//角色类型
                sSMJY = item.SubItems[3].Text.ToString().Trim();
                sjsmc = item.SubItems[1].Text.ToString().Trim();//角色名称

                chushihuatree(test3);

                if (test3.Contains("系统管理"))
                {
                    getsetTreeNode(treeView11.Nodes);
                }
                else if (test3.Contains("安全管理"))
                {
                    getsetTreeNode(treeView12.Nodes);
                }
                else if (test3.Contains("审计管理"))
                {
                    getsetTreeNode(treeView13.Nodes);
                }
                else if (test3.Contains("业务"))
                {
                    getsetTreeNode(treeView14.Nodes);
                }

                string sql = string.Format(@"Select * From juesequanxian where jsqx_jsmc='{0}' and jsqx_jslx='{1}' ", test2, test3);
                Query mquery = new Query();
                string squanxian = mquery.GetDBString(sql, "jsqx_qx");
                if (squanxian != "")
                {
                    cquanxian = squanxian.Split(new char[] { ',' });
                    if (cquanxian.Length > 0)
                    {
                        for (int i = 0; i < cquanxian.Length; i++)
                        {
                            for (int j = 0; j < 20; j++)
                            {
                                if (cquanxian[i].Contains(cche[j].Text))
                                {
                                    cche[j].Checked = true;
                                    break;
                                }
                            }
                        }
                    }
                }

              /* string test1 = item.SubItems[0].Text.ToString().Trim();
               string test2 = item.SubItems[1].Text.ToString().Trim();
               string test3 = item.SubItems[2].Text.ToString().Trim();
               sSMJY = item.SubItems[3].Text.ToString().Trim();
               if (test3.Contains("安全管理"))
               {
                   Bind_TreeView(treeView2, "安全管理", "安全管理");
               }
               else if (test3.Contains("系统管理"))
               {
                   Bind_TreeView(treeView2, "系统管理", "系统管理");
               }
               else if (test3.Contains("审计管理"))
               {
                   Bind_TreeView(treeView2, "审计管理", "审计管理");
               }
               else if (test3.Contains("业务办理"))
               {
                   Bind_TreeView(treeView2, "业务办理", "业务办理");
               }
               else if (test3.Contains("默认角色"))
               {
                   Bind_TreeView(treeView2, "默认角色", "默认角色");
               }*/
               /* string test1 = item.SubItems[0].Text.ToString().Trim();
                string test2 = item.SubItems[1].Text.ToString().Trim();//角色名称
                string test3 = item.SubItems[2].Text.ToString().Trim();//角色类型
                sSMJY = item.SubItems[3].Text.ToString().Trim();

                chushihua(test3);
                string sql = string.Format(@"Select * From juesequanxian where jsqx_jsmc='{0}' and jsqx_jslx='{1}' ", test2, test3);
                Query mquery = new Query();
                string squanxian = mquery.GetDBString(sql, "jsqx_qx");
                if (squanxian != "")
                {
                    cquanxian = squanxian.Split(new char[] { ',' });
                    if (cquanxian.Length > 0)
                    {
                        for (int i = 0; i < cquanxian.Length; i++)
                        {
                            for (int j = 0; j < 20; j++)
                            {
                                if (cquanxian[i].Contains(cche[j].Text))
                                {
                                    cche[j].Checked = true;
                                    break;
                                }
                            }
                        }
                    }
                }*/
            }
        }

        public void chushihua(string sjslx)
        {
            for (int i = 0; i < 20; i++)
            {
                cche[i].Visible = false;
                cche[i].Checked = false;
            }
            if (sjslx.Contains("系统管理"))
            {
                checkBox1.Visible = true;
                checkBox1.Text = "部门管理";
                checkBox2.Visible = true;
                checkBox2.Text = "用户管理";
                checkBox3.Visible = true;
                checkBox3.Text = "功能管理";
                checkBox4.Visible = true;
                checkBox4.Text = "角色管理";
                checkBox5.Visible = true;
                checkBox5.Text = "配置管理";
                checkBox6.Visible = true;
                checkBox6.Text = "设备管理";
            }
            else if (sjslx.Contains("安全管理"))
            {
                checkBox1.Visible = true;
                checkBox1.Text = "激活用户";
                checkBox2.Visible = true;
                checkBox2.Text = "解锁用户";
                checkBox3.Visible = true;
                checkBox3.Text = "终端解锁";
                checkBox4.Visible = true;
                checkBox4.Text = "用户注销";

            } if (sjslx.Contains("审计管理"))
            {
                checkBox1.Visible = true;
                checkBox1.Text = "安全审计";
                checkBox2.Visible = true;
                checkBox2.Text = "安全日志查询";
                checkBox3.Visible = true;
                checkBox3.Text = "核心功能日志审计";
                checkBox4.Visible = true;
                checkBox4.Text = "接口日志查询";
                checkBox5.Visible = true;
                checkBox5.Text = "异常操作预警";

            }
            if (sjslx.Contains("业务"))
            {
                checkBox1.Visible = true;
                checkBox1.Text = "检测数据查询";
                checkBox2.Visible = true;
                checkBox2.Text = "查验数据查询";
                checkBox3.Visible = true;
                checkBox3.Text = "检测预警管理";
                checkBox4.Visible = true;
                checkBox4.Text = "检测数据统计";
                checkBox5.Visible = true;
                checkBox5.Text = "检测过程查询";
                checkBox6.Visible = true;
                checkBox6.Text = "检测收费管理";
                checkBox7.Visible = true;
                checkBox7.Text = "车辆登录";
                checkBox8.Visible = true;
                checkBox8.Text = "车辆出表";

                checkBox9.Visible = true;
                checkBox9.Text = "收费管理";

                checkBox10.Visible = true;
                checkBox10.Text = "时间同步";

                checkBox11.Visible = true;
                checkBox11.Text = "检测合格标准查看";

                checkBox12.Visible = true;
                checkBox12.Text = "检测类别管理";

                checkBox13.Visible = true;
                checkBox13.Text = "外检项目定义";

                checkBox14.Visible = true;
                checkBox14.Text = "检验项目信息下载";

                checkBox15.Visible = true;
                checkBox15.Text = "检测线备案信息下载";

                checkBox16.Visible = true;
                checkBox16.Text = "检验机构备案信息下载";



            }
        }
        public void chushihuatree(string sjslx)
        {
           
            if (sjslx.Contains("系统管理"))
            {
                treeView11.Visible = true;
                treeView12.Visible = false;
                treeView13.Visible = false;
                treeView14.Visible = false;

            }
            else if (sjslx.Contains("安全管理"))
            {
                treeView11.Visible = false;
                treeView12.Visible = true;
                treeView13.Visible = false;
                treeView14.Visible = false;

            } if (sjslx.Contains("审计管理"))
            {
                treeView11.Visible = false;
                treeView12.Visible = false;
                treeView13.Visible = true;
                treeView14.Visible = false;

            }
            if (sjslx.Contains("业务"))
            {
                treeView11.Visible = false;
                treeView12.Visible = false;
                treeView13.Visible = false;
                treeView14.Visible = true;

            }
        }
        /// <summary>
          /// TreeView的数据绑定父节点函数
          /// </summary>
          /// <param name="treeview">TreeView控件ID名称</param>
          /// <param name="text">树控件要显示的文本的字段名称</param>
         public void Bind_TreeView(TreeView treeview, string text,string sleixing)//父节点
         {
             DataSet DsPNode = new DataSet();
             DsPNode = QueryEX(string.Format("select * from fulei where fl_leixing='{0}' order by fl_neirong desc ", sleixing));
             treeview.Nodes.Clear();
             for (int i = 0; i < DsPNode.Tables[0].Rows.Count; i++)
             {
                 TreeNode rootnode = new TreeNode();//创建根节点
                 rootnode.Text = DsPNode.Tables[0].Rows[i]["fl_neirong"].ToString().Trim();
                 treeview.Nodes.Add(rootnode);//在添加完节点的内容之后，当然要添加根节点，
                 if (sSMJY.Contains("是"))
                 {
                     rootnode.Checked = true;
                    
                 }
                 else
                 {
                     if (rootnode.Text.Contains("检验业务管理"))
                     {
                         rootnode.Checked = false;
                     }
                     else
                     {
                         rootnode.Checked = true;
                     }
                 }
                 if (text != "默认角色")
                 {
                     CreateChildNodes(rootnode, text, "deptname", rootnode.Text);//DsPNode.Tables[0].Rows[i][text].ToString()
                 }
                // if()
              }
         }

        /// <summary>
          /// TreeView的数据绑定子节点函数
          /// </summary>
          /// <param name="treenode">上一级节点</param>
          /// <param name="parentName">数据表中字段名</param>
          /// <param name="text">树控件要显示的文本的字段名称</param>
          /// <param name="index">查询语句关键字</param>
           public void CreateChildNodes(TreeNode treenode, string parentName, string text, string index)
          {
              DataSet DsCNode = QueryEX("SELECT * from zilei where zl_leixing= '" + index + "'");
              for (int i = 0; i < DsCNode.Tables[0].Rows.Count; i++)
              {
                  TreeNode childnode = new TreeNode();
                  childnode.Text = DsCNode.Tables[0].Rows[i]["zl_neirong"].ToString().Trim();
                  treenode.Nodes.Add(childnode);
                //  childnode.Checked = true;
                  if (sSMJY.Contains("是"))
                  {
                      childnode.Checked = true;
                      
                  }
                  else
                  {
                      if (index.Contains("检验业务管理"))
                      {
                          childnode.Checked = false;
                      }
                      else 
                      {
                          childnode.Checked = true;
                      }
                     
                  }
                  CreateGrandsonNodes(childnode, text, "name", childnode.Text);//DsCNode.Tables[0].Rows[i][text].ToString()
              }
         }

        //三级节点的添加
         /// <summary>
          /// TreeView的数据绑定三级节点函数
         /// </summary>
          /// <param name="treenode">上一级节点</param>
          /// <param name="parentName">数据表中字段名</param>
          /// <param name="text">树控件要显示的文本的字段名称</param>
          /// <param name="index">查询语句关键字</param>
           public void CreateGrandsonNodes(TreeNode treenode, string parentName, string text, string index)
          {
              DataSet ds = QueryEX("SELECT * from zilei2 where zl_leixing= '" + index + "'");
              for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
              {
                  TreeNode childnode = new TreeNode();
                  childnode.Text = ds.Tables[0].Rows[i]["zl_neirong"].ToString().Trim();
     
                  treenode.Nodes.Add(childnode);
                 // childnode.Checked = true;

                  if (sSMJY.Contains("是"))
                  {
                      childnode.Checked = true;
                     
                  }
                  else
                  {
                      if (index.Contains("跨站车辆管理") || index.Contains("检验过程监管") || index.Contains("车辆黑名单管理") || index.Contains("车辆申请"))
                      {
                          childnode.Checked = false;
                      }
                      else
                      {
                          childnode.Checked = true;
                      }
                  }
              }
             
             
         }
        /// <summary>
          /// 执行查询语句，返回DataSet
          /// </summary>
          /// <param name="sqlstr">SQL查询语s句</param>
          /// <returns>DataSet</returns>
           public DataSet QueryEX(string sqlstr)
          {
              Query objquery = new Query();

              return objquery.GetDataSet(sqlstr);
             
         }

          private void ufn_CheckChildren(TreeNode node)
          {
              if (node.Nodes.Count > 0)
              {
                  foreach (TreeNode n in node.Nodes)
                  {
                      n.Checked = node.Checked;
                      this.ufn_CheckChildren(n);
                  }
              }
          }

          private void treeView2_AfterCheck(object sender, TreeViewEventArgs e)
          {
             // this.ufn_CheckChildren(e.Node);
          }

          private void juesequanxian_Load(object sender, EventArgs e)
          {
              inicbx(lstUser, "USERE");
          }
    }
}
